//
//  ZXForgetController.m
//  ZhuoxinAgriculture
//
//  Created by KKING on 2016/11/26.
//  Copyright © 2016年 stone. All rights reserved.
//

#import "ZXForgetController.h"
#import "HMNavigationController.h"
#import "ZXLoginViewController.h"
@interface ZXForgetController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *originalPasswordTextField;
@property (weak, nonatomic) IBOutlet UITextField *freshPasswordTextField;
@property (weak, nonatomic) IBOutlet UIButton *nextButton;

@end

@implementation ZXForgetController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.nextButton setBackgroundImage:[SNUtils imageWithColor:HexRGB(0x57A736)] forState:UIControlStateNormal];
    [self.nextButton setBackgroundImage:[SNUtils imageWithColor:HexRGB(0x9ACD8B)] forState:UIControlStateDisabled];
    
    self.nextButton.enabled = NO;
    
    self.originalPasswordTextField.delegate = self;
    self.freshPasswordTextField.delegate = self;
    
    [self addObserver];
    
}
- (void)addObserver
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange:) name:UITextFieldTextDidChangeNotification object:nil];
    
}
- (void)textChange:(NSNotification *)notify
{
    NSLog(@"%@",notify);
    if (self.freshPasswordTextField.text.length>0 && self.originalPasswordTextField.text.length>0) {
        self.nextButton.enabled = YES;
    }
    
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


- (IBAction)updatepasswordButtonClick:(UIButton *)sender {
    
    if (self.originalPasswordTextField.text.length <= 0) {
        [SNProgressManager showToView:self.view message:@"请输入原密码" afterDelay:2];
        return;
    }
    
    if (self.freshPasswordTextField.text.length <= 0) {
        
        [SNProgressManager showToView:self.view message:@"请输入新密码" afterDelay:2];
        return;
    }
    
    if (self.freshPasswordTextField.text.length>0 && self.originalPasswordTextField.text.length>0) {
        
        NSString * time = [NSString stringWithFormat:@"%ld",(NSInteger)[[NSDate date] timeIntervalSince1970]];
        
        [[ZXHTTPManager sharedManager] updatepasswordWithTime:time userId:kUserInfo.userId phone:kUserInfo.account password:self.originalPasswordTextField.text newPassword:self.freshPasswordTextField.text completed:^(NSDictionary *jsonDict, NSError *error) {
            
            logError;
            
            NSLog(@"%@",jsonDict);
            
            [SNProgressManager showToView:self.view message:jsonDict[@"message"] afterDelay:2];
            //             HUDNormal(jsonDict[@"message"]);
            //             HUDLoading(jsonDict[@"message"]);
            
            if ([jsonDict[@"status"] longValue] == 0) {
                HUDNormal(jsonDict[@"message"]);
                [self.navigationController popViewControllerAnimated:YES];
            }
            
        }];
    }else{
        NSLog(@"%@",@"失败");
    }
    
}


@end
